//
//  myResmeCellExpRowViews.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/4/8.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class myResumeCellExpRowViews: UIView {
    
    
    var companyName:UILabel?
    var jobName:UILabel?
    var jobTag:UILabel?//标签
    var jobDescLab:UILabel?
    
    var timeLab:UILabel?
    
    let SameLabTag = 333
    
    var dotView:UIImageView?
    var divideLine:UIView?
    
    var model = myResumeCellExperienceModels_details() {
        
        didSet{
            updateFrame()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setDefaultContent()
        
        NSOperationQueue.bk_performBlock({ (obj) in
          
        
            let labsArr = ["梵蒂冈","大噶短裤"]
            let Y:CGFloat = (CGRectGetMaxY(self.jobName!.frame))
            
            UILabel.creatLabsFromArrTags(labsArr, labsTag: self.SameLabTag, labH: 20, font: LDFont.Font13, toView: self, originX: 25, originY: Y, marginX: 13, marginY: 13)
            
            let labsCount = self.viewWithTag(labsArr.count+self.SameLabTag-1) as! UILabel
            
            self.jobDescLab?.snp_updateConstraints(closure: { (make) in
                make.top.equalTo(labsCount.snp_bottom).offset(Margin_V)
                make.height.equalTo(self.model.jobDescH)
                
            })
            
        }, afterDelay: 2)

        
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setDefaultContent() {
        
        self.frame = LDFrame.ScreenBounds
        
        companyName = UILabel.setDetailsLab("互联网 | 杭州乐电网络科技有限公司", fontSize: 14, titleColor:LDFontColor.DeepGray,Alignment: .Left)
        timeLab = UILabel.setDetailsLab("2008 - 2015", fontSize: 14, titleColor:LDFontColor.DeepGray,Alignment: .Left)
        jobName = UILabel.setDetailsLab("设计师 | 视觉设计", fontSize: 14, titleColor:LDFontColor.DeepGray,Alignment: nil)
        jobDescLab = UILabel.setDetailsLab("", fontSize: 13, titleColor: LDFontColor.LightGray,Alignment: nil)
        
        
        divideLine = UIView()
        divideLine?.backgroundColor = LDLineColorLightGray
        
        dotView = UIImageView(image: UIImage(named: "green_dot"))
        
        addSubview(dotView!)
        addSubview(divideLine!)
        addSubview(companyName!)
        addSubview(timeLab!)
        addSubview(jobName!)
        addSubview(jobDescLab!)
        
        
        dotView?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(0)//.offset(LDMargin_V*3)
            make.left.equalTo(0)
            make.size.equalTo(CGSizeMake(10, 10))
        })
        
        divideLine?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(dotView!.snp_bottom)
            make.centerX.equalTo(dotView!)
            make.bottom.equalTo(0)
            make.width.equalTo(0.5)
        })
        
        
        timeLab?.snp_makeConstraints(closure: { (make) in
            
            make.centerY.equalTo(dotView!)
            make.left.equalTo(dotView!.snp_right).offset(LDMargin_V*2)
            make.height.equalTo(20)
            make.right.equalTo(LDMargin_V)
            
        })
        
        companyName?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo((timeLab?.snp_bottom)!).offset(LDMargin_V)
            make.left.height.right.equalTo(timeLab!)
        })
        
        jobName?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo((companyName?.snp_bottom)!).offset(LDMargin_V)
            make.left.height.right.equalTo(timeLab!)
            
//            print(jobName?.frame)

        })
        
//        /** 生成便签栏目 */
//        UILabel.setMoreSameLabs(self, SameLabTag: SameLabTag) { () -> Int in
//            return 9
//        }
        
       
        
//        //
//        jobDescLab?.snp_makeConstraints(closure: { (make) in
//            make.left.equalTo(companyName!)
//            make.right.equalTo(0)
//            make.height.equalTo(0)
//        })
    }
    
    //更新
    func updateFrame() {
        
        companyName?.text = "\(model.companyname) | \(model.industryname)"
        jobName?.text = "\(model.jobtitle) | \(model.department)"
        timeLab?.text = "\(model.startdate) - \(model.enddate)"
        
        //显示labs个数
        let Y = LDMargin_V*4 + LDCellFrame.LabsH// + 18// + LDMargin_V*2//jobLabH
        
        var jobstrenthStrArr = [String]()
        
        if DDClientInfoManager.clientType() == DDClientType.Cattle {
            
            for item in model.jobstrenths {
                jobstrenthStrArr.append(item["tag"] as! String)
            }
            
        } else {
            jobstrenthStrArr = model.jobstrenths as? [String] ?? [String]()
        }
        
        UILabel.setupLabFrame(jobstrenthStrArr, SameLabTag: self.SameLabTag,
                                     textSize: CGSizeMake(CGFloat.max, 15), font: LDFont.Font11,
                                     target: self,
                                     labMargin: (Origin_X: LDMargin_V*4, Origin_Y: Y, Margin_X: Margin_V*2, Margin_Y: Margin_V*2, LabH: LDFrame.sampleLabsInCellH))
        //更改labs的默认颜色
        for i in 0..<model.jobstrenths.count {
            
            let views = viewWithTag(SameLabTag+i) as! UILabel
            views.layer.borderColor = LDIconColor.LightGray.CGColor
            views.textColor = LDFontColor.DeepGray
        }
        
        
        //工作经历文本
        jobDescLab?.attributedText = String.getAttrFormString(lineH: 6, text: model.jobdesc,font:LDFont.Font14)
        
        //按照cell的标签个数判断位置
        let countLabs = model.jobstrenths.count
        if countLabs > 0 {
            let labsCount = self.viewWithTag(countLabs+SameLabTag-1) as! UILabel
            
            jobDescLab?.snp_updateConstraints(closure: { (make) in
                make.top.equalTo(labsCount.snp_bottom).offset(Margin_V)
                make.height.equalTo(model.jobDescH)
            })
            
        } else {
            
            jobDescLab?.snp_updateConstraints(closure: { (make) in
                make.top.equalTo(jobName!.snp_bottom).offset(Margin_V)
                make.height.equalTo(model.jobDescH)
            })
        }
    }
    
    
    
}
